package control.panel
{
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import view.components.ModulLoader;
	import view.panel.HelpSubPanel;
	import view.panel.help.HelpPanel;
	import utils.OpenWindow;
	import global.GameState;

	public class HelpPanelControl extends PanelControl
	{
		private var helpModulLoader:ModulLoader;
		private var helpSubModulLoader:ModulLoader;
		private var currentSideIndex:int;
		
		public function HelpPanelControl()
		{
			initPanel();
			initModul();
		}
		
		private function initModul():void
		{
			helpModulLoader = new ModulLoader(500,200);
			helpModulLoader.x = 7;
			helpModulLoader.y = 20;
			_view.helpPanel.addChild(helpModulLoader);
			
			helpModulLoader.addEventListener(Event.COMPLETE,onHelpLoadComplete);
			
			helpSubModulLoader = new ModulLoader(585,425);
			helpSubModulLoader.x = 7;
			helpSubModulLoader.y = 20;
			
			_view.helpSubPanel.addChild(helpSubModulLoader);
			helpSubModulLoader.addEventListener(Event.COMPLETE,onHelpSubLoadComplete);
		}
		
		private function onHelpSubLoadComplete(event:Event):void
		{
			if (currentSideIndex > 0)
			{
				gotoSide(currentSideIndex);
			}
		}
		
		private function onHelpLoadComplete(event:Event):void
		{
			helpModulLoader.content.addEventListener("base",onBut);
			helpModulLoader.content.addEventListener("rule",onBut);
			helpModulLoader.content.addEventListener("card",onBut);
			helpModulLoader.content.addEventListener("score",onBut);
			helpModulLoader.content.addEventListener("tutor",onBut);
			helpModulLoader.content.addEventListener("fast",onBut);
		}
		
		private function onBut(event:Event):void
		{
			switch (event.type)
			{
				case "base":
					gotoSide(1);
					_view.helpSubPanel.open();
					break;
				case "rule":
					gotoSide(2);
					_view.helpSubPanel.open();
					break;
				case "card":
					gotoSide(3);
					_view.helpSubPanel.open();
					break;
				case "score":
					gotoSide(4);
					_view.helpSubPanel.open();
					break;
				case "tutor":
					OpenWindow.openWindow(GameState.getTutorLink(),"_top");
					break;
				case "fast":
					_view.helpSubPanel.close();
					_view.helpPanel.close();
					_control.hallControl.onFastEnter();
					break;
			}
		}
		
		private function gotoSide(index:int):void
		{
			if (helpSubModulLoader.isComplete)
			{
				helpSubModulLoader.content.gotoSide(index);
			}
			else
			{
				currentSideIndex = index;
			}
		}
		
		private function initPanel():void
		{
			_view.helpPanel = new HelpPanel(this);
			_view.helpSubPanel = new HelpSubPanel(this);
			
			_view.helpPanel.addEventListener(Event.CLOSE,onHelpPanelClose);
		}
		
		private function onHelpPanelClose(event:Event):void
		{
			_control.hallControl.startOrigalHint();
		}
		
		public function open(isSkip:Boolean = false):void
		{
			if ( !helpModulLoader.isLoading && !helpModulLoader.isComplete)
			{
				helpModulLoader.load("resource/helpmain.swf");
			}
			
			if ( !helpSubModulLoader.isLoading && !helpSubModulLoader.isComplete)
			{
				helpSubModulLoader.load("resource/help.swf");
			}
			
			if (isSkip)
			{
				gotoSide(1);
				_view.helpSubPanel.open();
			}
			else
			{
				_view.helpPanel.open();
			}
			
			_control.hallControl.stopAllHint();
		}
		
		
	}
}